﻿using CIS.Common.Dtos.ReportDtos;
using CIS.Common.Enums;
using CIS.Common.Grpc;
using CIS.Device.Common.Events;
using CIS.Device.Modules.Commons;
using CIS.Device.Modules.IndexModules.Models;
using Grpc.Core;
using Grpc.Net.Client;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace CIS.Device.Modules.IndexModules.ViewModels
{
    public class IndexViewModel : BindableBase
    {        
        private readonly IEventAggregator _eventAggregator;
        public IndexViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            Init();
        }


        #region 属性
        private string? unitStatus;
        /// <summary>
        /// 设备状态
        /// </summary>
        public string? UnitStatus
        {
            get { return unitStatus; }
            set { unitStatus = value; }
        }

        private string runBackGroundColor;
        public string RunBackGroundColor
        {
            get { return runBackGroundColor; }
            set { runBackGroundColor = value; RaisePropertyChanged(nameof(RunBackGroundColor)); }
        }

        private string idleBackGroundColor;
        public string IdleBackGroundColor
        {
            get { return idleBackGroundColor; }
            set { idleBackGroundColor = value; RaisePropertyChanged(nameof(idleBackGroundColor)); }
        }

        private string manualBackGroundColor;
        public string ManualBackGroundColor
        {
            get { return manualBackGroundColor; }
            set { manualBackGroundColor = value; RaisePropertyChanged(nameof(ManualBackGroundColor)); }
        }

        private string downBackGroundColor;
        public string DownBackGroundColor
        {
            get { return downBackGroundColor; }
            set { downBackGroundColor = value; RaisePropertyChanged(nameof(downBackGroundColor)); }
        }

        private PortModel selectedPort;
        /// <summary>
        /// 选中的 Port信息
        /// </summary>
        public PortModel SelectedPort
        {
            get { return selectedPort; }
            set { selectedPort = value; }
        }


        private List<PortModel> ports;

        public List<PortModel> Ports
        {
            get { return ports; }
            set { ports = value; }
        }

        #endregion

        #region 命令

        private DelegateCommand<string> portControlCommand;
        /// <summary>
        /// 设备状态切换按钮命令
        /// </summary>
        public DelegateCommand<string> PortControlCommand
        {
            get
            {
                return portControlCommand ?? new DelegateCommand<string>(PortControlFunction);
            }
        }



        private DelegateCommand<string> unitControlCommand;
        /// <summary>
        /// 设备状态切换按钮命令
        /// </summary>
        public DelegateCommand<string> UnitControlCommand
        {
            get
            {
                return unitControlCommand ?? new DelegateCommand<string>(UnitControlFunction);
            }
        }


        #endregion

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            Ports = new List<PortModel>
            {
                new PortModel()
                {
                    PortId="P01",
                    Status = PortStatus.InUse,
                    SlotMap="111100000000000000000000000000",
                    CassetteId="AN0001",
                    JobCountinCassette=4
                },
                new PortModel()
                {
                    PortId="P02",
                    Status = PortStatus.Emty,
                    SlotMap="111100000000000000000000000000",
                },
                new PortModel()
                {
                    PortId="P03",
                    Status = PortStatus.Emty,
                    SlotMap="111100000000000000000000000000",
                }
            };
        }

        /// <summary>
        /// Port状态控制命令处理逻辑
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void PortControlFunction(string obj)
        {
            if (!string.IsNullOrEmpty(obj) && SelectedPort != null)
            {
                PortStatusChangeReportDto dto = new PortStatusChangeReportDto();
                dto.SourceId = "3AFS12-IND";
                dto.EventName = "PortStatusChangeReport";
                dto.PortId = SelectedPort.PortId;
                dto.CassetteID = SelectedPort.CassetteId;
                dto.JobCountInCassette = SelectedPort.JobCountinCassette;
                dto.SlotMap = SelectedPort.SlotMap;
                switch (obj)
                {
                    case "Chuck":
                        dto.PortStatus = PortStatus.InUse;
                        break;
                    case "UnChuck":
                        dto.PortStatus = PortStatus.UnLoadReady;
                        break;
                    case "ProcessEnd":
                        dto.PortStatus = PortStatus.UnLoadReady;
                        break;
                }
                GrpcMessage message = new GrpcMessage()
                {
                    IsRequest = false,
                    Name = dto.EventName,
                    Data = JsonConvert.SerializeObject(dto)
                };
                _eventAggregator.GetEvent<ReportEvent<GrpcMessage>>().Publish(message);

            }
        }

        /// <summary>
        /// 设备状态切换命令处理逻辑
        /// </summary>
        /// <param name="obj"></param>
        private void UnitControlFunction(string obj)
        {
            if(!string.IsNullOrEmpty(obj) && obj != UnitStatus)
            {
                UnitStatusChangeReportDto dto = new UnitStatusChangeReportDto();
                dto.SourceId = "3AFS12-IND";
                dto.EventName = "UnitStatusChangeReport";
                switch (obj)
                {
                    case "Run":
                        UnitStatus = "Run";
                        RunBackGroundColor = "Green";
                        ManualBackGroundColor = "White";
                        DownBackGroundColor = "White";
                        IdleBackGroundColor = "White";
                        dto.UnitStatus = 5;
                        dto.SubUnitCount = 0;
                        dto.ReasonCode = 5000;
                        
                        break;
                    case "Idle":
                        UnitStatus = "Idle";
                        RunBackGroundColor = "White";
                        ManualBackGroundColor = "White";
                        DownBackGroundColor = "White";
                        IdleBackGroundColor = "Yellow";
                        dto.UnitStatus = 4;
                        dto.SubUnitCount = 0;
                        dto.ReasonCode = 4005;
                        break;
                    case "Manual":
                        UnitStatus = "Manual";
                        ManualBackGroundColor = "#c26808";
                        RunBackGroundColor = "White";
                        DownBackGroundColor = "White";
                        IdleBackGroundColor = "White";
                        dto.UnitStatus = 1;
                        dto.SubUnitCount = 0;
                        dto.ReasonCode = 1001;
                        break;
                    case "Down":
                        UnitStatus = "Down";
                        ManualBackGroundColor = "White";
                        RunBackGroundColor = "White";
                        DownBackGroundColor = "Red";
                        IdleBackGroundColor = "White";
                        dto.UnitStatus = 2;
                        dto.SubUnitCount = 0;
                        dto.ReasonCode = 2002;
                        break;
                } 
                GrpcMessage message = new GrpcMessage()
                {
                    IsRequest = false,
                    Name = dto.EventName,
                    Data = JsonConvert.SerializeObject(dto)
                };
                _eventAggregator.GetEvent<ReportEvent<GrpcMessage>>().Publish(message);
            }
        }
        #endregion
    }
}

